<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>从proc文件系统中获取gateway的IP地址 - whowin - 发表我个人原创作品的技术博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="whowin" /><meta name="description" content="在linux的命令行下获取当前网络环境的gateway的IP并不是一件难事，常用的命令有ip route或者route -n，route -n是通过读取proc文件系统下的文件来从内核获取路由表的，但ip route是通过netlink来获取的路由表；本文将讨论如何编写程序从proc文件系统中获取路由表，并从路由表中获取gateway的IP地址，文章最后给出了完整的源程序，本文对初学者基本没有难度。
" /><meta name="keywords" content="linux, socket, hugo, dos" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://whowin.gitee.io/post/blog/network/0008-get-gateway-ip-from-proc-filesys/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.e3fea119b1980e848b03dffbeddb11dd0fba483eed0e5f11870fb8e31f145bbd.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="从proc文件系统中获取gateway的IP地址" />
<meta property="og:description" content="在linux的命令行下获取当前网络环境的gateway的IP并不是一件难事，常用的命令有ip route或者route -n，route -n是通过读取proc文件系统下的文件来从内核获取路由表的，但ip route是通过netlink来获取的路由表；本文将讨论如何编写程序从proc文件系统中获取路由表，并从路由表中获取gateway的IP地址，文章最后给出了完整的源程序，本文对初学者基本没有难度。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://whowin.gitee.io/post/blog/network/0008-get-gateway-ip-from-proc-filesys/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2023-02-05T16:43:29+08:00" />
<meta property="article:modified_time" content="2023-02-05T16:43:29+08:00" />

<meta itemprop="name" content="从proc文件系统中获取gateway的IP地址">
<meta itemprop="description" content="在linux的命令行下获取当前网络环境的gateway的IP并不是一件难事，常用的命令有ip route或者route -n，route -n是通过读取proc文件系统下的文件来从内核获取路由表的，但ip route是通过netlink来获取的路由表；本文将讨论如何编写程序从proc文件系统中获取路由表，并从路由表中获取gateway的IP地址，文章最后给出了完整的源程序，本文对初学者基本没有难度。"><meta itemprop="datePublished" content="2023-02-05T16:43:29+08:00" />
<meta itemprop="dateModified" content="2023-02-05T16:43:29+08:00" />
<meta itemprop="wordCount" content="2805">
<meta itemprop="keywords" content="Linux,Socket,网络编程,proc文件系统," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="从proc文件系统中获取gateway的IP地址"/>
<meta name="twitter:description" content="在linux的命令行下获取当前网络环境的gateway的IP并不是一件难事，常用的命令有ip route或者route -n，route -n是通过读取proc文件系统下的文件来从内核获取路由表的，但ip route是通过netlink来获取的路由表；本文将讨论如何编写程序从proc文件系统中获取路由表，并从路由表中获取gateway的IP地址，文章最后给出了完整的源程序，本文对初学者基本没有难度。"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

  <script async src="/js/busuanzi.pure.mini.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9724909319263152"
     crossorigin="anonymous"></script>


</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">WhoWin</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">文章归档</li>
      </a><a href="/article-categories/categories/">
        <li class="mobile-menu-item">文章分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">文章标签</li>
      </a><a href="/about/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">WhoWin</a>
  
  <div style="position:absolute; left: 80px; top: 75px; color: crimson">
      ———开源和分享是技术发展的源泉和动力；本博客所有文章均为原创
  </div>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">文章归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/article-categories/categories/">文章分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">文章标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">从proc文件系统中获取gateway的IP地址</h1>

      <div class="post-meta">
        <span class="post-time"> 2023-02-05 </span>
        <div class="post-category">
            <a href="/categories/linux/"> Linux </a>
            <a href="/categories/c-language/"> C Language </a>
            <a href="/categories/network/"> Network </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1-为什么要获取网关的ip地址">1. 为什么要获取网关的IP地址</a></li>
        <li><a href="#2-从proc文件系统中获取gateway的ip">2. 从proc文件系统中获取gateway的IP</a></li>
        <li><a href="#3-ip-route和route--n是如何获取路由表的">3. ip route和route -n是如何获取路由表的</a></li>
        <li><a href="#4-从proc文件系统中获取gateway的ip地址">4. 从proc文件系统中获取gateway的IP地址</a></li>
        <li><a href="#欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>在linux的命令行下获取当前网络环境的gateway的IP并不是一件难事，常用的命令有<code>ip route</code>或者<code>route -n</code>，<code>route -n</code>是通过读取proc文件系统下的文件来从内核获取路由表的，但<code>ip route</code>是通过netlink来获取的路由表；本文将讨论如何编写程序从proc文件系统中获取路由表，并从路由表中获取gateway的IP地址，文章最后给出了完整的源程序，本文对初学者基本没有难度。</p>
<h2 id="1-为什么要获取网关的ip地址">1. 为什么要获取网关的IP地址</h2>
<blockquote>
<p>以前写过一些与raw socket有关的文章，在使用raw socket发送报文的时候，有时是需要自己构建以太网报头的，以太网报头中是要填写源地址和目的地址的MAC地址的，源地址的MAC地址就是本机的MAC地址，可以使用ioctl()获得，但是目的地址的MAC地址就不好办了，如果是局域网内，我们可以通过arp获取目的地址的MAC地址，因为我们是知道目的地址的IP的，但是如果目的地址在局域网外，我们就要在以太网报头中填写gateway的MAC地址，这时候我们就一定要知道<strong>gateway的IP地址</strong>，然后通过arp cache获取gateway的MAC地址；</p>
</blockquote>
<blockquote>
<p>获得了gateway的IP地址以后，可以很容易地从本地arp cache中获得MAC地址的，因为gateway的MAC地址一定会在本地arp cache中，有关如何在操作arp cache，可以参考我的另一篇文章<a href="https://blog.csdn.net/whowin/article/details/129791465">《如何用C语言操作arp cache》</a></p>
</blockquote>
<h2 id="2-从proc文件系统中获取gateway的ip">2. 从proc文件系统中获取gateway的IP</h2>
<ul>
<li>
<p>Linux下的proc文件系统是一个虚拟文件系统，所谓虚拟文件系统指的是它并不是一个存放在硬盘上的目录，而是内核建立的用于让用户空间了解内核运行状态或者调试的运行时的文件系统；</p>
</li>
<li>
<p>内核的路由表存放在 <code>/proc/net/route</code> 文件下，在终端上用命令<code>cat /proc/net/route</code>可以很容易地打印出路由表；下面是在我的机器上看到的结果：</p>
<p><img src="https://whowin.gitee.io/images/180008/cat-proc-net-route.png" alt="routing table"></p>
</li>
</ul>
<hr>
<ul>
<li><strong>/proc/net/route文件的结构</strong>
<ul>
<li>第一行为表头，显示每一列的名称，从第二行起为路由数据，每行代表一条路由；</li>
<li>每列之间的分隔符为TAB(\t)；</li>
<li>第1列：Iface，为接口名称(Interface Name);</li>
<li>第2列：Destination，为目标网段或者目标主机的IP，以字符串表达的一个16进制的32位(4字节)数字(比如0X0103A8C0将表示为字符串&quot;0103A8C0&quot;)，把这个字符串按照16进制转换成一个32位数字，则表达着一个IP地址；</li>
<li>第3列：Gateway，为gateway的IP，与第2列的表达形式一样；</li>
<li>第4列：Flags，为一个标志，每一位代表一个标志，定义在linux/route.h中，如下：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define RTF_UP          0x0001        </span><span class="cm">/* route usable                 */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_GATEWAY     0x0002        </span><span class="cm">/* destination is a gateway     */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_HOST        0x0004        </span><span class="cm">/* host entry (net otherwise)   */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_REINSTATE   0x0008        </span><span class="cm">/* reinstate route after tmout  */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_DYNAMIC     0x0010        </span><span class="cm">/* created dyn. (by redirect)   */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_MODIFIED    0x0020        </span><span class="cm">/* modified dyn. (by redirect)  */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_MTU         0x0040        </span><span class="cm">/* specific MTU for this route  */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_MSS         RTF_MTU       </span><span class="cm">/* Compatibility :-(            */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_WINDOW      0x0080        </span><span class="cm">/* per route window clamping    */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_IRTT        0x0100        </span><span class="cm">/* Initial round trip time      */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define RTF_REJECT      0x0200        </span><span class="cm">/* Reject route                 */</span><span class="cp">
</span></span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>看着挺多，但其实常用的很少，就是前三个，<strong>RTF_UP</strong>表示该路由可用，<strong>RTF_GATEWAY</strong>表示该路由为一个网关，组合在一起就是3，表示一个可用的网关；</li>
<li>第5列：RefCnt，为该路由被引用的次数，Linux内核中没有使用这个；</li>
<li>第6列：Use，该路由被查找的次数；</li>
<li>第7列：Metric，到目的地的&quot;距离&quot;，通常以&quot;跳数&quot;表示，所谓&quot;跳数&quot;可以理解为要经过的网关的数量，实际数字并不准确；</li>
<li>第8列：Mask，网络掩码；</li>
<li>第9列：MTU，路由上可以传送的最大数据包；</li>
<li>第10列：Window，TCP窗口的大小，只在AX.25网络上使用</li>
<li>第11列：IRTT，通过这条路由的初始往返时间，只在AX.25网络上使用；</li>
<li>可以使用在线手册 <code>man route</code> 了解更详细的信息；</li>
<li>当Destination为&quot;00000000&quot;时，表示任意目的地址；</li>
<li>当Gateway为&quot;00000000&quot;时，表示不需要经过网关，比如本地局域网中的目的地址，Gateway字段应该是&quot;00000000&quot;。</li>
</ul>
</li>
</ul>
<h2 id="3-ip-route和route--n是如何获取路由表的">3. ip route和route -n是如何获取路由表的</h2>
<ul>
<li>
<p>使用命令 <code>ip route</code> 可以获得路由表，下面是运行截图：</p>
<p><img src="https://whowin.gitee.io/images/180008/screenshot-of-ip-route.png" alt="screenshot of executing ip route"></p>
</li>
</ul>
<hr>
<ul>
<li>
<p><code>ip route</code> 是使用netlink获取路由表的，我们可以使用strace命令跟踪 <code>ip route</code> 的执行，然后在输出中查找 <strong>/proc/net/route</strong> 和<strong>RTM_GETROUTE</strong>，从而确定这个命令是如何获得路由表的</p>
</li>
<li>
<p>如果不清楚宏<strong>RTM_GETROUTE</strong>的含义，请参考另一篇文章<a href="https://blog.csdn.net/whowin/article/details/129409890">《linux下使用netlink获取gateway的IP地址》</a></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">strace -o ip.txt ip route
</span></span><span class="line"><span class="cl">grep RTM_GET_ROUTE ip.txt
</span></span><span class="line"><span class="cl">grep /proc/net/route ip.txt
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p><code>-o</code> 选项表示跟踪输出结果写入文件ip.txt中，然后从文件ip.txt中分别查找 <code>RTM_GETROUTE</code> 和 <code>/proc/net/route</code></p>
</li>
<li>
<p>下面是运行结果的截图</p>
<p><img src="https://whowin.gitee.io/images/180008/screenshot-of-tracing-ip-route.png" alt="trace ip route"></p>
</li>
</ul>
<hr>
<ul>
<li>可以很清晰第看到，在执行 <code>ip route</code> 的过程中，使用RTM_GETROUTE作为参数向socket上发送了一条获取路由表的信息，而找不到关于文件 <code>/proc/net/route</code> 的任何信息，由此可以肯定 <code>ip route</code> 是使用netlink获取的路由表；</li>
<li>有关使用netlink获取gateway IP的方法，可以参考另一篇文章<a href="https://blog.csdn.net/whowin/article/details/129409890">《linux下使用netlink获取gateway的IP地址》</a></li>
</ul>
</li>
<li>
<p>使用命令<code>route -n</code>也是可以获得路由表的，下面是运行截图：</p>
<p><img src="https://whowin.gitee.io/images/180008/screenshot-of-route-n.png" alt="screenshot of route -n"></p>
</li>
</ul>
<hr>
<ul>
<li><code>route -n</code> 是通过读取proc文件系统中的文件 <code>/proc/net/route</code> 来获取路由表的，我们用同样的方法来跟踪一下 <code>route -n</code> 的运行情况：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">strace -o route.txt route -n
</span></span><span class="line"><span class="cl">grep RTM_GET_ROUTE route.txt
</span></span><span class="line"><span class="cl">grep /proc/net/route route.txt
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p>下面是运行结果截图</p>
<p><img src="https://whowin.gitee.io/images/180008/screenshot-of-tracing-route-n.png" alt="trace route -n"></p>
</li>
</ul>
</li>
</ul>
<hr>
<ul>
<li>跟踪结果告诉我们在执行 <code>route -n</code> 时，文件 <code>/proc/net/route</code> 被读取，但是却没有找到使用 RTM_GETROUTE 从内核获取路由表的迹象，由此可以断定 <code>route -n</code> 是通过proc文件系统获取的路由表。</li>
</ul>
<h2 id="4-从proc文件系统中获取gateway的ip地址">4. 从proc文件系统中获取gateway的IP地址</h2>
<ul>
<li>
<p>内核将路由表放在proc文件系统下，文件名为：/proc/net/route</p>
</li>
<li>
<p>读取文件<code>/proc/net/route</code>即可获得路由表，从中找到gateway这一行就可以了，按以下步骤：</p>
<ol>
<li>打开文件/proc/net/route</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define ROUTING_TABLE   &#34;/proc/net/route&#34;
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>
</span></span><span class="line"><span class="cl"><span class="n">FILE</span> <span class="o">*</span><span class="n">fp</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="n">ROUTING_TABLE</span><span class="p">,</span> <span class="s">&#34;r&#34;</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="2">
<li>读取一行不做任何处理，跳过路由表的表头行；</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define BUF_SIZE        128
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>
</span></span><span class="line"><span class="cl"><span class="kt">char</span> <span class="n">line</span><span class="p">[</span><span class="n">BUF_SIZE</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="n">fgets</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">BUF_SIZE</span><span class="p">,</span> <span class="n">fp</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="3">
<li>每次读取一行，检查其是否为gateway的记录，如果是，将gateway字段转换成32位的IP地址再转换成字符串；</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">char</span> <span class="o">*</span><span class="n">ifname</span><span class="p">,</span> <span class="o">*</span><span class="n">dest_ip</span><span class="p">,</span> <span class="o">*</span><span class="n">gw_ip</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span> <span class="p">(</span><span class="n">fgets</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">BUF_SIZE</span><span class="p">,</span> <span class="n">fp</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">ifname</span>  <span class="o">=</span> <span class="n">strtok</span><span class="p">(</span><span class="n">line</span> <span class="p">,</span> <span class="s">&#34;</span><span class="se">\t</span><span class="s">&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">dest_ip</span> <span class="o">=</span> <span class="n">strtok</span><span class="p">(</span><span class="nb">NULL</span> <span class="p">,</span> <span class="s">&#34;</span><span class="se">\t</span><span class="s">&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">gw_ip</span>   <span class="o">=</span> <span class="n">strtok</span><span class="p">(</span><span class="nb">NULL</span> <span class="p">,</span> <span class="s">&#34;</span><span class="se">\t</span><span class="s">&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="p">(</span><span class="n">ifname</span> <span class="o">!=</span> <span class="nb">NULL</span> <span class="o">&amp;&amp;</span> <span class="n">dest_ip</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="p">(</span><span class="n">strcmp</span><span class="p">(</span><span class="n">dest_ip</span><span class="p">,</span> <span class="s">&#34;00000000&#34;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span><span class="n">gw_ip</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="k">struct</span> <span class="n">in_addr</span> <span class="n">addr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">                <span class="n">sscanf</span><span class="p">(</span><span class="n">gw_ip</span><span class="p">,</span> <span class="s">&#34;%x&#34;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">addr</span><span class="p">.</span><span class="n">s_addr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">                <span class="n">strcpy</span><span class="p">(</span><span class="n">gw</span><span class="p">,</span> <span class="n">inet_ntoa</span><span class="p">(</span><span class="n">addr</span><span class="p">));</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="k">break</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="4">
<li>关闭文件</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>下面是源代码，文件名：<a href="https://gitee.com/whowin/whowin/blob/blog/sourcecodes/180008/get-gateway-proc.c">get-gateway-proc.c</a>(<strong>点击文件名下载源程序</strong>)</p>
</li>
<li>
<p>这段程序也没什么好解释的，唯一要说明的地方是gateway路由的确定，这里是以目的地址为&quot;00000000&quot;作为判断，前面讨论过，目的地址为&quot;00000000&quot;的含义为任意目的地址；</p>
</li>
<li>
<p>编译：<code>gcc -Wall get-gateway-proc.c -o get-gateway-proc</code></p>
</li>
<li>
<p>运行：<code>./get-gateway-proc</code></p>
</li>
<li>
<p>下面是运行截图</p>
<p><img src="https://whowin.gitee.io/images/180008/screenshot-get-gateway-proc.png" alt="screenshot of executing get-gateway-proc"></p>
</li>
</ul>
<h2 id="欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></h2>
<hr>
<p><strong>欢迎访问我的博客：https://whowin.cn</strong></p>
<p><strong>email: <a href="mailto:hengch@163.com">hengch@163.com</a></strong></p>
<p><img src="https://whowin.gitee.io/images/qrcode/sponsor-qrcode.png" alt="donation"></p>
<!--gitee
[article01]:https://whowin.gitee.io/post/blog/network/0014-handling-arp-cache/
[article02]:https://whowin.gitee.io/post/blog/network/0009-get-gateway-ip-via-netlink/
-->
<!-- CSDN
[img01]:https://img-blog.csdnimg.cn/img_convert/312c99b788170ca756b88a813f510d5a.png
[img02]:https://img-blog.csdnimg.cn/img_convert/a330b274e9c11aff7c23f11398627476.png
[img03]:https://img-blog.csdnimg.cn/img_convert/28f0ff045103dd4f11e02b77f5aab6e0.png
[img04]:https://img-blog.csdnimg.cn/img_convert/0f111a4943e67ff7529d033157995394.png
[img05]:https://img-blog.csdnimg.cn/img_convert/f63f190b63a47c7ba759311aebe7e7d9.png
[img06]:https://img-blog.csdnimg.cn/img_convert/050b5313d0cb7dc94f96fd8d8cf6ef17.png
-->
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">whowin</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2023-02-05
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/linux/">Linux</a>
          <a href="/tags/socket/">Socket</a>
          <a href="/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a>
          <a href="/tags/proc%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F/">proc文件系统</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/blog/network/0015-send-magic-packet-via-raw-socket/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">使用raw socket发送magic packet</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/blog/network/0009-get-gateway-ip-via-netlink/">
            <span class="next-text nav-default">linux下使用netlink获取gateway的IP地址</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/blog/network/0008-get-gateway-ip-from-proc-filesys/" class="leancloud_visitors" data-flag-title="从proc文件系统中获取gateway的IP地址">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'OFCGzCfJRUglzOdzrqMGkbTR-gzGzoHsz',
        appKey: 'v7P29kPAEbsmaavaYPNhGhnF',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:hengch@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://whowin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>
<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2022 - 
    2024<span class="heart"><i class="iconfont icon-heart"></i></span><span>whowin</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>








</body>
</html>
